package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 08 - 04
 * Time:10:55
 */

import java.util.ArrayList;
import java.util.Stack;

/***
 * 从尾到头打印链表——考虑到栈的先进后出的特点
 */
/*class ListNode{
    int val;
    ListNode next = null;

    ListNode(int val){
        this.val = val;
    }
}*/
public class Test3 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack stack = new Stack();
        ListNode cur = listNode;
        while(cur != null){
            stack.add(cur.val);
            cur = cur.next;
        }

        ArrayList<Integer> list = new ArrayList<>();
        while(!stack.isEmpty()){
            list.add((Integer) stack.pop());
        }
        return list;
    }
}
